class WorkspacesController < ApplicationController
  before_filter :login_required
  layout 'workspace_layout'
  
  def profile
    @user = User.find_by_id(session[:user_id])
  end
  
  def statement
    @user = User.find_by_id(session[:user_id])
  end
  
  def badday
    @user = User.find_by_id(session[:user_id])
  end
  
  def question
    @user = User.find_by_id(session[:user_id])
  end
  
  def station
    @category = Category.all
    @user = User.find_by_id(session[:user_id])
  end
  
  def issue
    @user = User.find_by_id(session[:user_id])
  end
  
  def vote
    @category = Category.all
    voter_state = Array.new
    voter_bad = Array.new
    voter_quest = Array.new
    
    states = StatementVoter.all
    states.each do |state|
      if state.member_voter == session[:user_id]
        voter_state.push(state.statement_id)
      else
        voter_state.push(0)
      end
    end
    bads = BaddayVoter.all
    bads.each do |bad|
      if bad.member_voter == session[:user_id]
        voter_bad.push(bad.badday_id)
      else
        voter_bad.push(0)
      end
    end
    quests = QuestionVoter.all
    quests.each do |quest|
      if quest.member_voter == session[:user_id]
        voter_quest.push(quest.question_id)
      else
        voter_quest.push(0)
      end
    end
    if !params[:cat].nil?
      @user = User.find_by_id(session[:user_id])
      @statement = Statement.find(:all,:conditions =>[" id NOT IN(?) AND category_id = ? ",voter_state,params[:cat]],:order=>"created_at DESC")
      @badday = Badday.find(:all,:conditions =>[" id NOT IN(?) AND category_id = ?  ",voter_bad,params[:cat]],:order=>"created_at DESC")
      @question = Question.find(:all,:conditions =>[" id NOT IN(?) AND category_id =?",voter_quest,params[:cat]],:order=>"created_at DESC")
      if !params[:id].nil?
        @comment = Comment.find_by_id(params[:id])
      end
      @category_id = params[:cat]
    else
      @user = User.find_by_id(session[:user_id])
      @statement = Statement.find(:all ,:conditions =>["id NOT IN(?)", voter_state],:order=>"created_at DESC")
      @badday = Badday.find(:all ,:conditions =>["id NOT IN(?)", voter_bad],:order=>"created_at DESC")
      @question = Question.find(:all ,:conditions =>["id NOT IN(?)", voter_quest],:order=>"created_at DESC")
      if !params[:id].nil?
        @comment = Comment.find_by_id(params[:id])
      end
    end
  end
  
  
  def group
    @user = User.find_by_id(session[:user_id])
    @data = Array.new
    @user = User.find_by_id(session[:user_id])
    @user_group = UserGroup.find(:all,:conditions =>"user_id = #{@user.id}")
    if !@user_group.nil?
      @user_group.each do |user_group|
        @data.push(user_group.group_id)
      end
    end
    if !@data.empty?
      @group = Group.find(:all,:conditions =>["id NOT IN (?)",@data.to_a])
    else
      @group = Group.all
    end
    @user_all = User.all
  end
  
  
  
  def station_statement
    @category = Category.all
    @user = User.find_by_id(session[:user_id])
    if !params[:cat].nil?
      @statement = Statement.find(:all,:conditions =>["category_id = ?",params[:cat]],:order=>"created_at DESC")
    else
      @statement = Statement.find(:all,:order=>"created_at DESC")
    end
  end
  
  def station_badday
    @category = Category.all
    @user = User.find_by_id(session[:user_id])
    if !params[:cat].nil?
      @badday = Badday.find(:all,:conditions =>["category_id = ?",params[:cat]])
    else
      @badday = Badday.find(:all,:order=>"created_at DESC")
    end
  end
  
  def station_question
    @category = Category.all
    @user = User.find_by_id(session[:user_id])
    if !params[:cat].nil?
      @question = Question.find(:all,:conditions =>["category_id = ?",params[:cat]],:order=>"created_at DESC")
    else
      @question = Question.find(:all,:order=>"created_at DESC")
    end
  end
  
  def statement_vote_up
    @user = User.find_by_id(session[:user_id])
    @statement = Statement.find_by_id(params[:format])
    vote = StatementVoter.new
    vote.member_voter = @user.id
    @statement.statement_voters << (vote)
    vote.save
    @statement.update_attribute(:agree,Integer(@statement.agree) + 1)
    redirect_to :action => "vote"
  end
  
  def statement_vote_down
    @user = User.find_by_id(session[:user_id])
    @statement = Statement.find_by_id(params[:format])
    vote = StatementVoter.new
    vote.member_voter = @user.id
    @statement.statement_voters << (vote)
    vote.save
    @statement.update_attribute(:disagree,Integer(@statement.disagree) + 1)
    redirect_to :action => "vote"
  end
  
  def badday_vote_up
    @user = User.find_by_id(session[:user_id])
    @badday = Badday.find_by_id(params[:format])
    vote = BaddayVoter.new
    vote.member_voter = @user.id
    @badday.badday_voters << (vote)
    vote.save
    @badday.update_attribute(:agree,Integer(@badday.agree) + 1)
    redirect_to :action => "vote"
  end
  
  def badday_vote_down
    @user = User.find_by_id(session[:user_id])
    @badday = Badday.find_by_id(params[:format])
    vote = BaddayVoter.new
    vote.member_voter = @user.id
    @badday.badday_voters << (vote)
    vote.save
    @badday.update_attribute(:disagree,Integer(@badday.disagree) + 1)
    redirect_to :action => "vote"
  end
  
  def question_vote
    if params[:commit] == "up"
      @user = User.find_by_id(session[:user_id])
      @question = Question.find_by_id(params[:format])
      @comment = Comment.new
      vote = QuestionVoter.new
      vote.member_voter = @user.id
      vote.question = (@question)
      vote.save
      @question.update_attribute(:agree,Integer(@question.agree) + 1)
      @comment.message = params[:answer]
      @comment.agree = 0
      @comment.disagree = 0
      @comment.commentator = @user.login
      @comment.question = (@question)
      @comment.save
    else
      @user = User.find_by_id(session[:user_id])
      @question = Question.find_by_id(params[:format])
      @comment = Comment.new
      vote = QuestionVoter.new
      vote.member_voter = @user.id
      vote.question = (@question)
      vote.save
      @question.update_attribute(:disagree,Integer(@question.disagree) + 1)
      @comment.message = params[:answer]
      @comment.agree = 0
      @comment.disagree = 0
      @comment.commentator = @user.login
      @comment.question = (@question)
      @comment.save
    end
    redirect_to :action => "vote"
  end
  
  def comment_vote_up
    @user = User.find_by_id(session[:user_id])
    @comment = Comment.find_by_id(params[:format])
    voter = CommentVoter.find(:all,:conditions =>["member_voter =? AND comment_id=?",@user.id,@comment.id])
    if voter.blank?
      @question = Question.find_by_id(@comment.question_id)
      vote_for_comment = CommentVoter.new
      vote_for_comment.comment_id = @comment.id
      vote_for_comment.member_voter = @user.id
      @comment.comment_voters << (vote_for_comment)
      vote_for_comment.save
      @comment.update_attribute(:agree,Integer(@comment.agree) + 1)
      flash[:comment_ss] = "Thank you for voting the comment"
    else
      flash[:comment_err] = "You have vote for this comment"
    end
    redirect_to :action => "station_question"
  end
  def comment_vote_down
    @user = User.find_by_id(session[:user_id])
    @comment = Comment.find_by_id(params[:format])
    voter = CommentVoter.find(:all,:conditions =>["member_voter =? AND comment_id=?",@user.id,@comment.id])
    if voter.blank?
      @question = Question.find_by_id(@comment.question_id)
      vote_for_comment = CommentVoter.new
      vote_for_comment.comment_id = @comment.id
      vote_for_comment.member_voter = @user.id
      @comment.comment_voters << (vote_for_comment)
      vote_for_comment.save
      @comment.update_attribute(:disagree,Integer(@comment.disagree) + 1)
      flash[:comment_ss] = "Thank you for voting the comment"
    else
      flash[:comment_err] = "You have vote for this comment"
    end
    redirect_to :action => "station_question"
  end
  
  def moderate_answer
    @comment = Comment.find(:all,:include =>[:comment_voters],:conditions =>["comment_voters.member_vote=? OR comment_voters.member_vote!=?,comment_voters.remote_vote='#{cookies[:user_ip]}'",0,session[:user_id]])
    if !@comment.blank?
      redirect_to :action => "vote" ,:id =>@comment.first.id
    else
      redirect_to :action => "vote"
    end
  end
  
  
  
  def view_mystatement
    @category = Category.all
    @user = User.find_by_id(session[:user_id])
    if !params[:cat].nil?
      @mystatement = Statement.find_all_by_user_id(@user.id,:conditions =>["category_id = ? ",params[:cat]],:order=>"created_at DESC")
    else
      @mystatement = Statement.find_all_by_user_id(@user.id,:order=>"created_at DESC")
    end
  end
  
  def view_mybadday
    @category = Category.all
    @user = User.find_by_id(session[:user_id])
    if !params[:cat].nil?
      @mybadday = Badday.find_all_by_user_id(@user.id,:conditions =>["category_id = ? ",params[:cat]],:order=>"created_at DESC")
    else
      @mybadday = Badday.find_all_by_user_id(@user.id,:order=>"created_at DESC")
    end
  end
  
  def view_myquestion
    @category = Category.all
    @user = User.find_by_id(session[:user_id])
    if !params[:cat].nil?
      @myquestion = Question.find_all_by_user_id(@user.id,:conditions =>["category_id = ? ",params[:cat]],:order=>"created_at DESC")
    else
      @myquestion = Question.find_all_by_user_id(@user.id,:order=>"created_at DESC")
    end
  end
  
  def statement_emailer
    
    if !params[:with].blank?
      statement = Statement.find_by_id(params[:with])
      emailer = StatementEmailer.new
      emailer.statement = (statement)
      emailer.save
    end
    if !params[:without].blank?
      emailer = StatementEmailer.find_by_statement_id(params[:without])
      emailer.destroy
    end
    redirect_to :action => :view_mystatement
  end
  
  def badday_emailer
    if !params[:with].blank?
      badday = Badday.find_by_id(params[:with])
      emailer = BaddayEmailer.new
      emailer.badday = (badday)
      emailer.save
    end
    if !params[:without].blank?
      emailer = BaddayEmailer.find_by_badday_id(params[:without])
      emailer.destroy
    end
    redirect_to :action => :view_mybadday
  end
  
  def question_emailer
    if !params[:with].blank?
      question = Question.find_by_id(params[:with])
      emailer = QuestionEmailer.new
      emailer.question = (question)
      emailer.save
    end
    if !params[:without].blank?
      emailer = QuestionEmailer.find_by_question_id(params[:without])
      emailer.destroy
    end
    redirect_to :action => :view_myquestion
  end
end
